//
//  LandMarkList.swift
//  ChinaLandMark
//
//  Created by Felix Hi on 2021/6/29.
//

import SwiftUI

struct LandMarkList: View {
    @State private var onlyShowFavorite = false
    @EnvironmentObject var userData: UserData
    
    var body: some View {
        NavigationView {
            List {
                Toggle(isOn: $onlyShowFavorite) {
                    Text("只显示收藏")
                }
                
                ForEach (userData.userLandmarks) { landmark in
                    if self.onlyShowFavorite {
                        if landmark.isFavorite {
                            NavigationLink(
                                destination: LandMarkDetail(landmark: landmark).environmentObject(self.userData)
                            ) {
                                LandMarkCell(landmark: landmark)
                            }
                        }
                    } else {
                        NavigationLink(
                            destination: LandMarkDetail(landmark: landmark)) {
                            LandMarkCell(landmark: landmark)
                        }
                    }
                }
            }.navigationBarTitle(Text("中国地标"))
        }
    }
}

struct LandMarkList_Previews: PreviewProvider {
    static var previews: some View {
        LandMarkList().environmentObject(UserData())
    }
}
